//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace OpenLS.Chart.UI
{
    partial class Chart3DViewportResources
    {
        public Chart3DViewportResources()
        {
                InitializeComponent();
        }
        static readonly Chart3DViewportResources instance = new Chart3DViewportResources();
        public static Style Chart3DViewportStyle { get { return (Style) instance["viewport3DStyle"]; } }
        void OnMouseDown (object sender, MouseButtonEventArgs e)
        {        
            var viewport3D = (Viewport3D) sender;
            Point mouseposition = e.GetPosition(viewport3D);
            var pointparams = new PointHitTestParameters(mouseposition);
            FrameworkElement owner = ChartElement.GetChartAreaOwner(viewport3D);
            ChartElement.SetSelectedObject(viewport3D, null);

            VisualTreeHelper.HitTest(viewport3D,
                null,
                                     rawresult => HTResult(owner, rawresult), 
                pointparams);
        }

        private static HitTestResultBehavior HTResult(DependencyObject owner, HitTestResult rawresult)
        {
          //  RayHitTestResult rayResult = rawresult as RayHitTestResult;
          //  if (rayResult == null)
          //      return HitTestResultBehavior.Continue;
            var rayMeshResult = rawresult as RayMeshGeometry3DHitTestResult;
            if (rayMeshResult == null)
                return HitTestResultBehavior.Continue;
            Model3D hitgeo = rayMeshResult.ModelHit;
            if (hitgeo == null)
                return HitTestResultBehavior.Continue;
            object s = ChartElement.GetObjectContext(hitgeo);
            if (s != null)
                ChartElement.SetSelectedObject(owner, s);
            return HitTestResultBehavior.Continue;
        }
    }
}
